//
//  CrazyDragViewController.m
//  CrazyDrag
//
//  Created by command.Zi on 14-4-29.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "CrazyDragViewController.h"
#import "CrazyDragAboutViewController.h"

@interface CrazyDragViewController() {
    int currentValue;
    int targetValue;
    int score;
    int round;
    int proValue;
    
    
}
- (IBAction)showAlert:(id)sender;
- (IBAction)sliderMoved:(id)sender;
- (IBAction)startOver:(id)sender;
- (IBAction)showInfo:(id)sender;
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) IBOutlet UILabel *targetLabel;
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutlet UILabel *roundLabel;
- (IBAction)stopView:(id)sender;


@end

@implementation CrazyDragViewController
@synthesize slider;
@synthesize targetLabel;
@synthesize scoreLabel;
@synthesize roundLabel;

- (void)startNewRound {
    targetValue = 1+(arc4random()%100);
    currentValue = 50;
    self.slider.value = currentValue;
}

- (void)updateLabbels {
    self.targetLabel.text = [NSString stringWithFormat:@"%d",targetValue];
    self.scoreLabel.text = [NSString stringWithFormat:@"%d",score];
    self.roundLabel.text = [NSString stringWithFormat:@"%d",round];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startNewGame];
    [self updateLabbels];
    
    //    [self setupsliderValue];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)showAlert:(id)sender {
    int difference = abs(currentValue -targetValue);
    int points = 100-difference;
    round ++;
    NSString *title;
    if (difference ==0) {
        title = @"完美";
        points+=100;
    }else if (difference <5) {
        if (difference == 1) {
            points+=50;
        }
        title = @"差一点";
    }else if (difference <10) {
        title = @"再来一次吧";
    }else {
        title = @"too bad";
    }
    score += points;
    NSString *message = [NSString stringWithFormat:@"滑动条的当前数值是：%d，你的得分是：%d",currentValue,points];
    [[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"再来" otherButtonTitles:nil, nil]show];
}


- (IBAction)sliderMoved:(UISlider *)sender {
    currentValue = (int)lround(sender.value);
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self startNewRound];
    [self updateLabbels];
}

- (IBAction)startOver:(id)sender {
    //添加过渡效果
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionFade;
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self startNewGame];
    [self updateLabbels];
    
    [self.view.layer addAnimation:transition forKey:nil];
}

- (void)startNewGame {
    score = 0;
    round = 0;
    [self startNewRound];
    
}

- (IBAction)showInfo:(id)sender {
    CrazyDragAboutViewController *controller = [[CrazyDragAboutViewController alloc]initWithNibName:@"CrazyDragAboutViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    [self presentViewController:controller animated:YES completion:nil];
    [self presentViewController:controller animated:YES completion:nil];
}

-(void)setupprogressViewValue {
    for (int i=1; i<100; i++) {
        [self.slider setValue:i];
        if (i >= 100) {
            i=1;
        }
    }
//    [self.slider setValue:10];
}
- (IBAction)stopView:(id)sender {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
